home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP09.ZIP / CHAP09 / PATRON / IDROPTGT.CPP < prev    next >
C/C++ Source or Header  |  1993-06-01  |  14KB  |  550 lines

  1. /*
  2.  * IDROPTGT.CPP
  3.  *
  4.  * Implementation of a DropTarget object for Patron
  5.  *
  6.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  7.  *
  8.  * Kraig Brockschmidt, Software Design Engineer
  9.  * Microsoft Systems Developer Relations
  10.  *
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "patron.h"
  17.  
  18.  
  19.  
  20. /*
  21.  * CDropTarget::CDropTarget
  22.  * CDropTarget::~CDropTarget
  23.  *
  24.  * Constructor Parameters:
  25.  *  pDoc            LPCPatronDoc of the window containing us.
  26.  */
  27.  
  28. CDropTarget::CDropTarget(LPCPatronDoc pDoc)
  29.     {
  30.     m_cRef=0;
  31.     m_pDoc=pDoc;
  32.  
  33.     m_pIDataObject=NULL;
  34.     return;
  35.     }
  36.  
  37.  
  38. CDropTarget::~CDropTarget(void)
  39.     {
  40.     return;
  41.     }
  42.  
  43.  
  44.  
  45.  
  46. /*
  47.  * CDropTarget::QueryInterface
  48.  * CDropTarget::AddRef
  49.  * CDropTarget::Release
  50.  *
  51.  * Purpose:
  52.  *  IUnknown members for CDropTarget object.
  53.  */
  54.  
  55. STDMETHODIMP CDropTarget::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  56.     {
  57.     *ppv=NULL;
  58.  
  59.     //Any interface on this object is the object pointer.
  60.     if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IDropTarget))
  61.         *ppv=(LPVOID)this;
  62.  
  63.     /*
  64.      * If we actually assign an interface to ppv we need to AddRef it
  65.      * since we're returning a new pointer.
  66.      */
  67.     if (NULL!=*ppv)
  68.         {
  69.         ((LPUNKNOWN)*ppv)->AddRef();
  70.         return NOERROR;
  71.         }
  72.  
  73.     return ResultFromScode(E_NOINTERFACE);
  74.     }
  75.  
  76.  
  77. STDMETHODIMP_(ULONG) CDropTarget::AddRef(void)
  78.     {
  79.     return ++m_cRef;
  80.     }
  81.  
  82. STDMETHODIMP_(ULONG) CDropTarget::Release(void)
  83.     {
  84.     ULONG           cRefT;
  85.  
  86.     cRefT=--m_cRef;
  87.  
  88.     if (0L==m_cRef)
  89.         delete this;
  90.  
  91.     return cRefT;
  92.     }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. /*
  99.  * CDropTarget::DragEnter
  100.  *
  101.  * Purpose:
  102.  *  Indicates that data in a drag operation has been dragged over our
  103.  *  window that's a potential target.  We are to decide if it's something
  104.  *  we're interested in or not.
  105.  *
  106.  * Parameters:
  107.  *  pIDataSource    LPDATAOBJECT providing the source data.
  108.  *  grfKeyState     DWORD flags indicating states of keys and mouse buttons.
  109.  *  pt              POINTL coordinates in the client space of the document.
  110.  *  pdwEffect       LPDWORD into which we'll place the appropriate effect
  111.  *                  flag for this point.
  112.  *
  113.  * Return Value:
  114.  *  SCODE           NOERROR
  115.  */
  116.  
  117. STDMETHODIMP CDropTarget::DragEnter(LPDATAOBJECT pIDataSource
  118.     , DWORD grfKeyState, POINTL pt, LPDWORD pdwEffect)
  119.     {
  120.     LPCPages        ppg=m_pDoc->m_pPG;
  121.     HWND            hWnd;
  122.     FORMATETC       fe;
  123.     STGMEDIUM       stm;
  124.     UINT            uRet;
  125.  
  126.     m_fFeedback=FALSE;
  127.     m_pIDataObject=NULL;
  128.  
  129.     if (!m_pDoc->FQueryPasteFromData(pIDataSource, &fe, NULL))
  130.         {
  131.         *pdwEffect=DROPEFFECT_NONE;
  132.         return NOERROR;
  133.         }
  134.  
  135.     //Check if this is a valid drop point.
  136.     uRet=ppg->UTestDroppablePoint(&pt);
  137.     ppg->m_uLastTest=uRet;
  138.  
  139.     if (UDROP_NONE==uRet)
  140.         *pdwEffect=DROPEFFECT_NONE;
  141.     else
  142.         {
  143.         //Default is move if we can, in fact drop here.
  144.         *pdwEffect=DROPEFFECT_MOVE;
  145.  
  146.         if (grfKeyState & MK_CONTROL)
  147.             *pdwEffect=DROPEFFECT_COPY;
  148.         }
  149.  
  150.     m_pIDataObject=pIDataSource;
  151.     m_pIDataObject->AddRef();
  152.  
  153.     /*
  154.      * Determine the size of the data, if we can.  The default is
  155.      * a small rectangle since we can't easily tell what size something
  156.      * will be if we're pulling in a metafile or bitmap.  It's not
  157.      * a good idea to render it here with ::GetData just to find that out.
  158.      * We only know the size if it's our own object in which case a
  159.      * ::GetData will be fast.
  160.      */
  161.  
  162.     if (fe.cfFormat==m_pDoc->m_cf)
  163.         {
  164.         if (SUCCEEDED(pIDataSource->GetData(&fe, &stm)))
  165.             {
  166.             LPPATRONOBJECT  ppo;
  167.             RECT            rc;
  168.  
  169.             ppo=(LPPATRONOBJECT)GlobalLock(stm.hGlobal);
  170.  
  171.             SetRect(&rc, (int)ppo->szl.cx, -(int)ppo->szl.cy, 0, 0);
  172.             RectConvertMappings(&rc, NULL, TRUE);
  173.             SETSIZEL(m_szl, rc.left, rc.top);
  174.  
  175.             m_ptPick=ppo->ptlPick;
  176.             m_fe=ppo->fe;
  177.  
  178.             GlobalUnlock(stm.hGlobal);
  179.             ReleaseStgMedium(&stm);
  180.             }
  181.         }
  182.     else
  183.         {
  184.         SETSIZEL(m_szl, 30, 30);
  185.         m_ptPick.x=0;
  186.         m_ptPick.y=0;
  187.         m_fe.cfFormat=0;
  188.  
  189.         //CHAPTER9MOD
  190.         /*
  191.          * Try to get CF_OBJECTDESCRIPTOR which might have a size and
  192.          * a pick point.  If it exists, then always use the point but
  193.          * still default to a 30*30 size if the sizes are zero.
  194.          */
  195.         uRet=RegisterClipboardFormat(CF_OBJECTDESCRIPTOR);
  196.         SETDefFormatEtc(fe, uRet, TYMED_HGLOBAL);
  197.  
  198.         if (SUCCEEDED(pIDataSource->GetData(&fe, &stm)))
  199.             {
  200.             LPOBJECTDESCRIPTOR  pOD;
  201.  
  202.             pOD=(LPOBJECTDESCRIPTOR)GlobalLock(stm.hGlobal);
  203.  
  204.             //Get the size, converting to LOMETRIC.
  205.             if (0!=pOD->sizel.cx && 0!=pOD->sizel.cy)
  206.                 XformSizeInHimetricToPixels(NULL, &pOD->sizel, &m_szl);
  207.  
  208.             //POINTL and SIZEL are interchangeable
  209.             XformSizeInHimetricToPixels(NULL, (LPSIZEL)&pOD->pointl
  210.                 , (LPSIZEL)&m_ptPick);
  211.  
  212.             GlobalUnlock(stm.hGlobal);
  213.             ReleaseStgMedium(&stm);
  214.             }
  215.         //End CHAPTER9MOD
  216.         }
  217.  
  218.  
  219.     //Bring the document window up front and show what a drop will do.
  220.     hWnd=m_pDoc->Window();
  221.     BringWindowToTop(hWnd);
  222.     UpdateWindow(hWnd);
  223.  
  224.     ppg->m_uVScrollCode=0xFFFF;
  225.     ppg->m_uHScrollCode=0xFFFF;
  226.     m_fPendingRepaint=FALSE;
  227.  
  228.     pt.x-=m_ptPick.x;
  229.     pt.y-=m_ptPick.y;
  230.  
  231.     m_ptLast=pt;
  232.     m_fFeedback=TRUE;
  233.     ppg->DrawDropTargetRect(&pt, &m_szl);
  234.  
  235.     return NOERROR;
  236.     }
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243. /*
  244.  * CDropTarget::DragOver
  245.  *
  246.  * Purpose:
  247.  *  Indicates that the mouse was moved inside the window represented
  248.  *  by this drop target.  This happens on every WM_MOUSEMOVE, so this
  249.  *  function should be very efficient.
  250.  *
  251.  * Parameters:
  252.  *  grfKeyState     DWORD providing the current keyboard and mouse states
  253.  *  pt              POINTL where the mouse currently is.
  254.  *  pdwEffect       LPDWORD in which to store the effect flag for this point.
  255.  *
  256.  * Return Value:
  257.  *  SCODE           NOERROR
  258.  */
  259.  
  260. STDMETHODIMP CDropTarget::DragOver(DWORD grfKeyState, POINTL pt
  261.     , LPDWORD pdwEffect)
  262.     {
  263.     LPCPages    ppg=m_pDoc->m_pPG;
  264.     UINT        uRet, uLast;
  265.     UINT        xPos, yPos;
  266.  
  267.     if (NULL==m_pIDataObject)
  268.         return NOERROR;
  269.  
  270.     //Check if this is still a valid point.  uRet is used below as well.
  271.     uRet=ppg->UTestDroppablePoint(&pt);
  272.  
  273.     if (UDROP_NONE==uRet)
  274.         *pdwEffect=DROPEFFECT_NONE;
  275.     else
  276.         {
  277.         //Store these before possibly ORing in DROPEFFECT_SCROLL
  278.         *pdwEffect=DROPEFFECT_MOVE;
  279.  
  280.         if (grfKeyState & MK_CONTROL)
  281.             *pdwEffect=DROPEFFECT_COPY;
  282.         }
  283.  
  284.     //If we haven't moved and we are not scrolling, then we're done.
  285.     if ((pt.x-m_ptPick.x==m_ptLast.x) && (pt.y-m_ptPick.y==m_ptLast.y)
  286.         && !((UDROP_INSETHORZ | UDROP_INSETVERT) & ppg->m_uLastTest))
  287.         {
  288.         return NOERROR;
  289.         }
  290.  
  291.     //Remove the last feedback rectangle.
  292.     if (m_fFeedback)
  293.         ppg->DrawDropTargetRect(&m_ptLast, &m_szl);
  294.  
  295.     uLast=ppg->m_uLastTest;
  296.     ppg->m_uLastTest=uRet;
  297.  
  298.     if (UDROP_NONE==uRet)
  299.         {
  300.         //If we are now an invalid point, better repaint as necessary
  301.         if (m_fPendingRepaint)
  302.             {
  303.             UpdateWindow(ppg->m_hWnd);
  304.             m_fPendingRepaint=FALSE;
  305.             }
  306.  
  307.         ppg->m_uVScrollCode=0xFFFF;
  308.         ppg->m_uHScrollCode=0xFFFF;
  309.         m_fFeedback=FALSE;
  310.         return NOERROR;
  311.         }
  312.  
  313.  
  314.     /*
  315.      * Scrolling is a little tricky:  We get a DragOver pulse even
  316.      * if we didn't move.  First we have to delay scrolling for
  317.      * ppg->m_uScrollDelay clock ticks which we can determine using
  318.      * GetTickCount.  Timers do not work here since we may not be
  319.      * yielding to our message loop.
  320.